package com.thirtydays.frame.module.main

import android.content.Context
import android.content.Intent
import android.graphics.Typeface
import android.os.Bundle
import android.os.PersistableBundle
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.FragmentUtils
import com.blankj.utilcode.util.TimeUtils
import com.luck.picture.lib.utils.ToastUtils.showToast
import com.thirtydays.frame.R
import com.thirtydays.frame.base.BaseActivity
import com.thirtydays.frame.databinding.ActivityMainBinding
import com.thirtydays.frame.module.home.view.HomeFragment
import com.thirtydays.frame.module.main.model.MainViewModel
import com.thirtydays.frame.module.mine.view.MineFragment
import com.thirtydays.frame.util.PermissionUtil
import com.thirtydays.frame.util.ext.clicks
import com.thirtydays.frame.util.ext.isNull
import com.thirtydays.frame.util.ext.textStringTrim
import com.xm.bluetoothkotlin.BluetoothManager

class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, MainActivity::class.java)
            context.startActivity(starter)
        }
    }

    private val homeFragment by lazy { HomeFragment.newInstance() }
    private val mineFragment by lazy { MineFragment.newInstance() }

    override fun init() {
        hideToolBar()
        addFragment()
        BluetoothManager.registerReceiver(this)
        switchFragment(mViewBinding.homeAtv.textStringTrim(), homeFragment)
    }

    private fun addFragment() {
        FragmentUtils.add(supportFragmentManager, homeFragment, R.id.flMain, true)
        FragmentUtils.add(supportFragmentManager, mineFragment, R.id.flMain, true)
    }


    override fun initListener() {
        with(mViewBinding) {
            //回退监听
        }
    }

    private var currentTabPosition = ""
    private fun switchFragment(index: String, fragment: Fragment) {
        if (currentTabPosition == index) {
            return
        }
        currentTabPosition = index
        switchView(index)
        hideFragment()
        if (fragment.isAdded) {
            FragmentUtils.show(fragment)
        } else {
            FragmentUtils.add(supportFragmentManager, fragment, R.id.flMain, fragment::class.java.simpleName, false, true)
        }
    }

    /**
     * 隐藏Fragment
     */
    private fun hideFragment() {
        FragmentUtils.getFragments(supportFragmentManager).forEachIndexed { _, fragment ->
            FragmentUtils.hide(fragment)
        }
    }

    /**
     * 切换选中状态
     */
    private fun switchView(index: String) {
        with(mViewBinding) {
            when (index) {

            }
        }
    }

    private var firstBackTime = 0L
    override fun onBackPressed() {
        if (TimeUtils.getNowMills() - firstBackTime > 2000) {
            showToast("再次点击退出应用")
            firstBackTime = TimeUtils.getNowMills()
        } else {
            finish()
        }
    }

    override fun onDestroy() {
        BluetoothManager.unregisterReceiver(this)
        super.onDestroy()
    }
}